//
//  SongsPageViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/12.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "SongsPageViewController.h"
#import "VideoViewController.h"
#import "SingerViewController.h"
#import "LGCLastViewController.h"
#import <MJRefresh.h>
#import "RankListDetailViewController.h"
#import "VideoPlayerViewController.h"
#import "FirstMoreViewController.h"
#import "AsiaNewSongViewController.h"
#import "MoreMusicViewController.h"
#import "MoreMusicDetailViewController.h"
#import "MoreActivityViewController.h"
#import "MoreExclusiveViewController.h"
#import <MBProgressHUD.h>
#import "WebViewViewController.h"
#import "SongsDetailPlayViewController.h"
#import "MoreNewestMusicViewController.h"



#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)


@interface SongsPageViewController ()<SDCycleScrollViewDelegate, UITableViewDataSource, UITableViewDelegate, MBProgressHUDDelegate,FirstTableViewDelege, SecondCollectionViewDelegate, AsiaDelegate, MusicObjectDelegate, ActivityDelegate, ExclusiveTableViewDelegate, mvTableViewCellDeledate, ThirdTableViewDelegate, NewestMusicDelegate>

@property (nonatomic, retain) NSMutableArray *totalArray;

@property (nonatomic, retain) NSMutableArray *imageArray;

@property (nonatomic, retain) NSMutableArray *titleArray;

@property (nonatomic, retain) NSMutableArray *btnImageArray;

@property (nonatomic, retain) NSMutableArray *btnNameArray;

@property (nonatomic, retain) NSMutableArray *dataArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) UIView *totalView;

@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation SongsPageViewController
- (void)dealloc
{
    [_dataArray release];
    [_tableView release];
    [_totalArray release];
    [_imageArray release];
    [_titleArray release];
    [_btnImageArray release];
    [_btnNameArray release];
    [_totalView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), WIDTH_PRO(30), HEIGHT_PRO(30));
    [button setImage:[UIImage imageNamed:@"iconfont-liebiao"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backToSlider) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"播放界面" style:UIBarButtonItemStylePlain target:self action:@selector(presentPlayView:)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createView];
    //数据解析
    [self loadData];
    
    
}
-(void)createView
{
    self.totalView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_PRO(300))];
    [self.view addSubview:self.totalView];
    [_totalView release];
    
}

-(void)loadData
{
    self.imageArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    self.totalArray = [NSMutableArray array];
    self.btnImageArray = [NSMutableArray array];
    self.btnNameArray = [NSMutableArray array];
    self.dataArray = [NSMutableArray array];
    
    self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    self.HUD.delegate = self;
    
    self.HUD.labelText = @"正在进行加载~";
    
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        NSDictionary *Dic = response;
        NSArray *array = [NSArray arrayWithArray:[Dic objectForKey:@"data"]];
        for (NSDictionary *dic in array) {
            SongsPageModel *songsPageModel = [[SongsPageModel alloc] initWithDictionary:dic];
        [self.totalArray addObject:songsPageModel];
        }
 
#pragma mark 轮播图
        NSDictionary *loopsDic = array[0];
        NSArray *loopsArray = [NSArray arrayWithArray:[loopsDic objectForKey:@"data"]];
        for (NSDictionary *dic in loopsArray) {
            NSString *imageUrl = [dic objectForKey:@"picUrl"];
            NSString *title = [dic objectForKey:@"name"];
            [self.imageArray addObject:imageUrl];
            [self.titleArray addObject:title];
        }
        
        SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, HEIGHT_PRO(64), SCREEN_WIDTH, HEIGHT_PRO(160)) imageURLStringsGroup:nil]; // 模拟网络延时情景
        cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        cycleScrollView2.delegate = self;
        cycleScrollView2.titlesGroup = self.titleArray;
        cycleScrollView2.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        cycleScrollView2.placeholderImage = [UIImage imageNamed:@"placeholder"];
        cycleScrollView2.autoScrollTimeInterval = 4.0;
        cycleScrollView2.imageURLStringsGroup = self.imageArray;
        [self.totalView addSubview:cycleScrollView2];
       // self.tableView.tableHeaderView = cycleScrollView2;
     
#pragma mark 分类按钮
        NSDictionary *btnDic = array[1];
        NSArray *btnArray = [NSArray arrayWithArray:[btnDic objectForKey:@"data"]];
        for (NSDictionary *dic in btnArray) {
            
            NSString *btnImageUrl = [dic objectForKey:@"picUrl"];
            NSString *btnName = [dic objectForKey:@"name"];
            [self.btnImageArray addObject:btnImageUrl];
            [self.btnNameArray addObject:btnName];
        }
        
        for (int i = 0; i < 4; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(WIDTH_PRO(35) + ((SCREEN_WIDTH - WIDTH_PRO(120)) / 4 + WIDTH_PRO(30)) * i, cycleScrollView2.frame.size.height + HEIGHT_PRO(80), WIDTH_PRO(20), HEIGHT_PRO(20));
            [button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.btnImageArray[i]]]] forState:UIControlStateNormal];
            button.tag = 1000 + i;
            [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.totalView addSubview:button];
            
            UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH_PRO(25) + ((SCREEN_WIDTH - WIDTH_PRO(120)) / 4 + WIDTH_PRO(33)) * i, cycleScrollView2.frame.size.height + HEIGHT_PRO(105), WIDTH_PRO(100), HEIGHT_PRO(20))];
            nameLabel.textColor = [UIColor grayColor];
            nameLabel.font = [UIFont systemFontOfSize:13];
            nameLabel.text = self.btnNameArray[i];
            [self.totalView addSubview:nameLabel];
            [nameLabel release];
            
        }
        
        [self.tableView reloadData];
        [self.HUD hide:YES afterDelay:2.0];
        
        [self createSubViews];
    }];
}

-(void)createSubViews
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, HEIGHT_PRO(-64), SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.tableHeaderView = self.totalView;
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        
//    }];
//    [self.tableView.mj_header beginRefreshing];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totalArray.count - 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0){
        return HEIGHT_PRO(240);
    }else if (indexPath.row == 1){
        return HEIGHT_PRO(390);
    }else if (indexPath.row == 2 || indexPath.row == 4 || indexPath.row == 5){
        return HEIGHT_PRO(210);
    }else if (indexPath.row == 6 || indexPath.row == 7) {
        return HEIGHT_PRO(160);
    }else if (indexPath.row == 8){
        return HEIGHT_PRO(310);
    }else if (indexPath.row == 10){
        return HEIGHT_PRO(150);
    }else if (indexPath.row == 9){
        return HEIGHT_PRO(220);
    }else{
        return HEIGHT_PRO(130);
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        SongsPageModel *songsModel = [_totalArray objectAtIndex:2];
        SongsPageTableViewCell *cell = [SongsPageTableViewCell songsPageTableViewCellWith:tableView];
        cell.songsPageModel = songsModel;
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 1){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:3];
        SongsListTableViewCell *cell = [SongsListTableViewCell songsListTableViewCellWith:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.songsPageModel = songsPageModel;
        return cell;
        
    }else if (indexPath.row == 2){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:4];
        ThirdTableViewCell *cell = [ThirdTableViewCell thirdTableViewCellWith:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.delegate = self;
        cell.songsPageModel = songsPageModel;
        return cell;

    }else if (indexPath.row == 3){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:6];
        AsiaNewSongTableViewCell *cell = [AsiaNewSongTableViewCell AsiaNewSongTableViewCellWith:tableView];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.songsPageModel = songsPageModel;
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 4){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:7];
        NewMusicTableViewCell *cell = [NewMusicTableViewCell newMusicTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        cell.delegate = self;
        return cell;
    }else if (indexPath.row == 5){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:8];
        MVTableViewCell *cell = [MVTableViewCell mvMusicTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        cell.delegate = self;
        return cell;
    
    }else if (indexPath.row == 6){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:9];
        HotestSongsTableViewCell *cell = [HotestSongsTableViewCell hotestSongsTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        return cell;
    }else if (indexPath.row == 7){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:10];
        EveryTenSongsTableViewCell *cell = [EveryTenSongsTableViewCell everyTenSongsTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        return cell;

    }else if (indexPath.row == 8){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:11];
        MusicObjectTableViewCell *cell = [MusicObjectTableViewCell musicTableViewCellWith:tableView];
        cell.delegate = self;
        cell.songsPageModel = songsPageModel;
        return cell;
    
    }else if (indexPath.row == 9){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:12];
        ActivityTableViewCell *cell = [ActivityTableViewCell activityTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        cell.delegate = self;
        return cell;
        
    }else if (indexPath.row == 10){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:13];
        ExclusiveTableViewCell *cell = [ExclusiveTableViewCell exclusiveTableViewCellWith:tableView];
        cell.songsPageModel = songsPageModel;
        cell.delegate = self;
        return cell;
    }
    return [[[UITableViewCell alloc] init] autorelease];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 6) {
      //  SongsPageModel *songsPageModel = [_totalArray objectAtIndex:9];
        RankListDetailViewController *rankList = [[RankListDetailViewController alloc] init];
        rankList.rankID = @"9";
        [self.navigationController pushViewController:rankList animated:YES];
        [rankList release];
    }else if (indexPath.row == 7){
        SongsPageModel *songsPageModel = [_totalArray objectAtIndex:10];
        SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
        songsDetail.rankID = [[songsPageModel.data[0] objectForKey:@"action"] objectForKey:@"value"];
        [self.navigationController pushViewController:songsDetail animated:YES];
        [songsDetail release];
    }
    
}

-(void)btnAction:(id)sender
{
    
    if (sender == [self.view viewWithTag:1000]) {
        
        RankListViewController *rankList = [RankListViewController new];
        [self.navigationController pushViewController:rankList animated:YES];
        [rankList release];

    }else if (sender == [self.view viewWithTag:1001]){
        SongsListSqureViewController *songsListSqure = [SongsListSqureViewController new];
        [self.navigationController pushViewController:songsListSqure animated:YES];
        [songsListSqure release];
        
    }else if (sender == [self.view viewWithTag:1002]){
        RadioStationViewController *radioStation = [RadioStationViewController new];
        [self.navigationController pushViewController:radioStation animated:YES];
        [radioStation release];
    }else{
        SingerViewController *singer = [[SingerViewController alloc] init];
        [self.navigationController pushViewController:singer animated:YES];
        [singer release];
    }
}

#pragma mark 代理方法
-(void)pushToAsiaNewSong
{
    AsiaNewSongViewController *asia = [[AsiaNewSongViewController alloc] init];
    [self.navigationController pushViewController:asia animated:YES];
    [asia release];
}

-(void)pushModel:(SongsModel *)model
{
    NSMutableArray *array = [NSMutableArray array];
    LGCLastViewController *lastView = [LGCLastViewController shareLastViewController];
    [array addObject:model];
    if (model.auditionList.count != 0) {
        if (model.auditionList.count == 3){
            lastView.playUrl = [model.auditionList[2] objectForKey:@"url"];
            lastView.strTitle = model.name;
        }else if (model.auditionList.count == 2){
            lastView.playUrl = [model.auditionList[1] objectForKey:@"url"];
            lastView.strTitle = model.name;
        }else {
            lastView.playUrl = [model.auditionList[0] objectForKey:@"url"];
            lastView.strTitle = model.name;
        }
    }

    lastView.tempArray = array;
    [self.navigationController presentViewController:lastView animated:YES completion:nil];
}

-(void)pushSongsListView
{
    SongsListSqureViewController *songs = [[SongsListSqureViewController alloc] init];
    [self.navigationController pushViewController:songs animated:YES];
    [songs release];
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    SongsPageModel *songsPageModel = _totalArray[0];
    if ([[[songsPageModel.data[index] objectForKey:@"action"] objectForKey:@"type"]  integerValue] == 1) {
        
        WebViewViewController *webView = [[WebViewViewController alloc] init];
        
        webView.strUrl = [[songsPageModel.data[index] objectForKey:@"action"] objectForKey:@"value"];
        
        [self.navigationController pushViewController:webView animated:YES];
        [webView release];
    }else if ([[[songsPageModel.data[index] objectForKey:@"action"] objectForKey:@"type"]  integerValue] == 5){
        
        SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
        songsDetail.rankID = [[songsPageModel.data[index] objectForKey:@"action" ] objectForKey:@"value"];
        [self.navigationController pushViewController:songsDetail animated:YES];
        [songsDetail release];
        
    }
}

-(void)backToSlider
{
    [self.sideMenuViewController presentLeftMenuViewController];
}

-(void)presentPlayView:(id)sender
{
    LGCLastViewController *lastView = [LGCLastViewController shareLastViewController];
    [self.navigationController presentViewController:lastView animated:YES completion:nil];
}

-(void)pushId:(NSString *)ID andName:(NSString *)name andPic:(NSString *)pic
{
    SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
    songsDetail.rankID = ID;
    [self.navigationController pushViewController:songsDetail animated:YES];
}

-(void)pushToVideo
{
    VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
    [self.navigationController pushViewController:videoPlayer animated:YES];
    [videoPlayer release];
}

-(void)pushFirstMoreView
{
    FirstMoreViewController *firstMore = [[FirstMoreViewController alloc] init];
    [self.navigationController pushViewController:firstMore animated:YES];
    [firstMore release];
}

-(void)pushToMusicMore
{
    MoreMusicViewController *moreMusic = [[MoreMusicViewController alloc] init];
    [self.navigationController pushViewController:moreMusic animated:YES];
    [moreMusic release];
}

-(void)pushNumber:(NSInteger)number
{
    SongsPageModel *songsPageModel = [_totalArray objectAtIndex:11];
    MoreMusicDetailViewController *moreMusicDetail = [[MoreMusicDetailViewController alloc] init];
    
    moreMusicDetail.strUrl = [[songsPageModel.data[number] objectForKey:@"action"] objectForKey:@"value"];
    
    [self.navigationController pushViewController:moreMusicDetail animated:YES];
    [moreMusicDetail release];
}

-(void)pushNum:(NSInteger)num
{
    SongsPageModel *songsPageModel = [_totalArray objectAtIndex:12];
    MoreMusicDetailViewController *moreMusicDetail = [[MoreMusicDetailViewController alloc] init];
    
    moreMusicDetail.strUrl = [[songsPageModel.data[num] objectForKey:@"action"] objectForKey:@"value"];
    
    [self.navigationController pushViewController:moreMusicDetail animated:YES];
    [moreMusicDetail release];

}

-(void)pushToActivityMore
{
    MoreActivityViewController *moreActivity = [[MoreActivityViewController alloc] init];
    [self.navigationController pushViewController:moreActivity animated:YES];
    [moreActivity release];
}

-(void)pushToMoreExclusive
{
    MoreExclusiveViewController *moreExclusive = [[MoreExclusiveViewController alloc] init];
    [self.navigationController pushViewController:moreExclusive animated:YES];
    [moreExclusive release];
}

-(void)push
{
    VideoViewController *videoView = [[VideoViewController alloc] init];
    [self.navigationController pushViewController:videoView animated:YES];
    [videoView release];
}

-(void)pushValue:(NSString *)value
{
    SongsDetailPlayViewController *songsDetail = [[SongsDetailPlayViewController alloc] init];
    songsDetail.rankID = value;
    [self.navigationController pushViewController:songsDetail animated:YES];
}

-(void)pushValueToWebView:(NSString *)value
{
    WebViewViewController *webView = [[WebViewViewController alloc] init];
    webView.strUrl = value;
    [self.navigationController pushViewController:webView animated:YES];
}

-(void)pushToMoreNewestMusic
{
    MoreNewestMusicViewController *moreNewestMusic = [[MoreNewestMusicViewController alloc] init];
    [self.navigationController pushViewController:moreNewestMusic animated:YES];
    [moreNewestMusic release];
}
-(void)pushVideoID:(NSString *)ID
{
    VideoPlayerViewController *videoPlayer = [[VideoPlayerViewController alloc] init];
    videoPlayer.vid = ID;
    [self.navigationController presentViewController:videoPlayer animated:YES completion:nil];;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
